// pages/work-order/order-proc/index.ts
import {detail} from '../../../api/work_order'
import {save, WorkOrderProc} from '../../../api/work_order_proc'
import { LENGYU_SUCCESS_CODE } from '../../../utils/request';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    workOrderId:"",
    formData: {
      title:"",
      workOrderId:"",
      currentStatus:"",
      imgUrlList:"",
      description:""
    },
 
    error:"",
    rules: [
      // {
      //   name: 'orderType',
      //   rules: {required: true, message: '工单类型'},
      // },
      // {
      //     name: 'carDate',
      //     rules: {required: true, message: '请选择日期'},
      // },
      // {
      //     name: 'carTime',
      //     rules: {required: true, message: '请选择时间'},
      // },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option:any) {
    // console.log(option)
    if(option && option.id){
      // console.log("页面参数id:",option)
      this.getDetailData(option.id);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  async getDetailData(id:string){
    let result = await detail(id);
    if(result.code == LENGYU_SUCCESS_CODE){
      this.setData({
        'formData.title':result.data.title,
        'formData.workOrderId':result.data.id,
        'formData.currentStatus':result.data.currentStatus,
        'formData.imgUrlList':'',
        'formData.description':"",
      })
    }
    else{
      //
    }
   
    console.log(result);
  },
  orderStatusChanged(e:any){
    console.log("工单状态变化：" ,e)
    this.setData({
      'formData.currentStatus':e.detail.value,
    })
  },
  bingDescriptionChange(e:any){
    console.log("处理说明",e)
    this.setData({
      'formData.description':e.detail.value,
    })
  },
  imgUrlListChanged(e:any){
    console.log("处理现场照片变更",e)
    this.setData({
      'formData.imgUrlList':(e.detail.imgUrlList.join(','))
    })
  },

  async submitForm(){
    this.selectComponent('#form').validate( async (valid:any, errors:any) =>{
      console.log('valid', valid, errors)
      if (!valid) {
          const firstError = Object.keys(errors)
          if (firstError.length) {
              this.setData({
                  error: errors[firstError[0]].message
              })
              wx.showToast({
                title: this.data.error,
                icon: 'error',
                duration: 2000
              })
          }
      } else {
        console.log(this.data.formData)
         let result = await save(this.data.formData as WorkOrderProc);
         console.log(result);
         if(result.code==LENGYU_SUCCESS_CODE){
          wx.navigateBack()
         }
      }
  })
  }
})